<template>
	<view class="page">
		<view class="topitem">
			<view class="top">
				<view class="toptit">账户余额： <text class="keti">{{userinfo.ketixian|capitalize}}</text>元</view>
				<view class="topcon">
					<view>
						<text>¥</text>
						<input type="text" @input="changejine" v-model="jine" placeholder="输入金额" />
					</view>
					<view>元</view>
				</view>
			</view>
			<view class="list">
				
				<view class="item" v-if="cardlist.length==1" @tap="toaddcard(cardlist)">
					{{cardlist[0].bank_name}} | {{cardlist[0].bank_card}} 
				</view>
				<view v-else class="item" @tap="addcard">请添加银行卡</view>
				<view class="item1">
					<view class="maleft">
						<text>验证码</text>
						<input type="text" v-model="code" />
					</view>
					
					<button class="code" :class="{ btnactive: !btnflag }" :disabled="btnflag == 0" @tap="getCode">{{ codemsg }}</button>
				</view>
				<view class="tip">
					手续费：{{fee}} 元
				</view>
			</view>
			<button class="queren" @tap="tixian">提交</button>
			
		</view>
		
		<view class="tip1">
			提现手续费{{userinfo.withdraw_rate}}%，第二天到账
		</view>
	</view>
</template>

<script>
		import { mapState } from 'vuex';
export default {
	data() {
		return {
			current: 0,
			jine: '',
			type: '',
			codemsg: '点击获取',
			btnflag: true,
			fee:0,
					
			code:'',
		};
	},
	computed: mapState({
		 userinfo: 'userinfo',
		 cardlist:'cardlist'
	}),
	onLoad() {
		var data = {
			token: uni.getStorageSync('token')
		}
		this.$store
			.dispatch('cardlist', data)
	},
		filters: {
		capitalize: function(value) {
			if (value != null) {
				var chazhi = 0;
				chazhi = parseFloat(value).toFixed(2);
				return chazhi;
			} else {
				return 0.00;
			}
	
		}
	},
	methods: {
		toaddcard(item) {
			var temp = this.cardlist[0];
			uni.navigateTo({
				url: '../../card/xgcard/index?type=' + 1+ "&username=" + temp['kh_xingming'] + "&bank=" + temp['bank_name'] + "&bankzhi=" + temp['bank_khh']+ "&bankbh=" + temp['bank_card'] + "&card_id=" + temp['card_id'] 
			})	
		},
		changejine:function(event) {
            this.fee = (event.target.value * this.userinfo.withdraw_rate)/100;
        },
		addcard(){
			uni.navigateTo({
				url:'../../card/cardlist/index'
			})
		},
			getCode() {
			
			this.code = '';
			
			var tellReg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
			var data = {
				tel: this.userinfo.tel,
				lb: 3
			};
			if (tellReg.test(this.userinfo.tel)) {
				this.$store.dispatch('getcode', data)
				.then(res => {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					});
					if (res.data.code == 1) {
						var num = 120;
						var timer = setInterval(() => {
							this.btnflag = false;
							this.codemsg = '' + num + 's';
							num--;
							if (num < 1) {
								this.btnflag = true;
								this.codemsg = '点击获取';
								clearInterval(timer);
							}
						}, 1000);
					}
				})
		
				
			} else {
				uni.showToast({
					title: '请登录有效账户',
					icon: 'none'
				});
			}
		},
		tixian() {
			if(this.jine>0){
				var data = {
					token: uni.getStorageSync('token'),
					// yh_id:this.userinfo.yh_id,
					chujin_jine:this.jine,
					card_id:this.cardlist[0].card_id,
					sms_code: this.code
				}
				this.$store.dispatch('withdraw', data).then(res => {
					uni.showToast({
						title: res.data.msg,
						icon:'none'
					});
					if (res.data.code == 1) {
						setTimeout(()=>{
							uni.navigateBack({
							    delta: 1
							});
							// uni.switchTab({
							// 	url:'../../money/index'
							// })
						},1000)
					}
				})
			}
			else{
				uni.showToast({
					title: '请输入充值金额',
					icon: 'none'
				});
			}
			
		}
	}
};
</script>

<style lang="scss" scoped src="./index.scss"></style>
